<template>
  <div class="app-container">

      <el-row :gutter="20" class="header">
        <el-col :span="7">
          <el-input placeholder="请输入用户名..." v-model="queryForm.query" clearable ></el-input>
        </el-col>
        <el-button type="primary" :icon="Search" @click="initUserList">搜索</el-button>
        <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>
        <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
          <template #reference>
           <el-button type="danger" :disabled="delBtnStatus" :icon="Delete" >批量删除</el-button>
          </template>
         </el-popconfirm>
      </el-row>
      <div class="table-container">
        <el-table
          :data="tableData"
          stripe
          border
          
          @selection-change="handleSelectionChange"  fit>
        <el-table-column type="selection" width="40%" />
        <el-table-column prop="id" label="序号" width="50%" align="center"/>
        <el-table-column prop="userName" label="用户名" width="80%" align="center"/>
        <el-table-column prop="roles" label="角色" width="80%" align="center">
          <template v-slot="scope">
            <el-tag size="small" type="warning" >{{scope.row.role.name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="150%" align="center"/>
        <el-table-column prop="phone" label="手机号" width="100%" align="center"/>
        <el-table-column prop="status" label="状态？" width="120%" align="center" >
          <template v-slot="{row}" >
            <el-switch  v-model="row.status" @change="statusChangeHandle(row)" active-text="正常"
                       inactive-text="禁用" active-value="0" inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="120%" align="center"/>
        <el-table-column prop="updateTime" label="修改时间" width="120%" align="center"/>
        <el-table-column prop="remark" label="备注" width="260%" />
        <el-table-column prop="action" label="操作" width="260%" align="center">
        <template v-slot="scope" >
          <el-popconfirm title="您确定要对这个用户重置密码吗？" @confirm="handleResetPassword(scope.row.id)">
            <template #reference>
              <el-button  type="warning" :icon="RefreshRight">重置密码</el-button>
            </template>
          </el-popconfirm>

          <el-button  type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)">编辑</el-button>

            <el-popconfirm title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button  type="danger" :icon="Delete" >删除</el-button>
              </template>
            </el-popconfirm>


        </template>
       </el-table-column>
      </el-table>
      </div>
      <el-pagination
          v-model:currentPage="queryForm.pageNum"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[5,10, 20, 30,]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle" @initUserList="initUserList"></Dialog>
  

</template>

<script setup>
import { Search ,Delete,DocumentAdd ,Edit, Tools, RefreshRight} from '@element-plus/icons-vue'
import { ref,getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from './components/dialog.vue'
import {getUsers,deleteUser,resetPassword,updateStatus} from '@/api/user'

const queryForm=ref({
  
  pageNum:1,
  pageSize:10,
  query:'',
})

const total=ref(0)

const tableData = ref([])

const id=ref(-1)

const sysRole=ref()

const imageDialogValue=ref({})

const dialogVisible=ref(false)

const roleDialogVisible=ref(false)

const dialogTitle=ref('')

const multipleSelection=ref([])

const delBtnStatus=ref(true)

const handleSelectionChange=(selection)=>{
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value=selection;
  delBtnStatus.value=selection.length==0;
}


const initUserList=async()=>{
  const res=await getUsers(queryForm.value);
  tableData.value=res.userList;
  console.log(tableData.value);
  total.value=res.total;
}


initUserList();

const handleSizeChange=(pageSize)=>{
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initUserList();
}

const handleCurrentChange=(pageNum)=>{
  queryForm.value.pageNum=pageNum;
  initUserList();
}


const statusChangeHandle=async (row)=>{
  let res=await updateStatus(row.id,row.status);
    ElMessage({
      type: 'success',
      message: '操作成功!'
    })
}

const handleDialogValue=(userId)=>{
  if(userId){
    id.value=userId;
    dialogTitle.value="用户修改"
  }else{
    id.value=-1;
    dialogTitle.value="用户添加"
  }
  dialogVisible.value=true
}

// const handleRoleDialogValue=(userId,role)=>{
//   console.log("userId="+userId)
//   id.value=userId;
//   sysRole.value=role;
//   roleDialogVisible.value=true
// }

const handleDelete=async (id)=>{
  var ids = []
  if(id){
    ids.push(id)
  }else{
    multipleSelection.value.forEach(row=>{
      ids.push(row.id)
    })
  }
  const res=await deleteUser(ids)
    ElMessage({
      type: 'success',
      message: '操作成功!'
    })
    initUserList();
  
}

const handleResetPassword=async (id)=>{
  const res=await resetPassword(id)
    ElMessage({
      type: 'success',
      message: '操作成功!'
    })
    initUserList();
}


</script>

<style  scoped>

.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}

.app-container {
  width: 100%;
  padding: 0;
}

.table-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); /* 自动适应列宽 */
  width: 100%;
}

.el-table {
  width: 100%;
  max-height: 800px;
}
.el-pagination{
  float: left;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell{
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;

}

.el-tag--small {
  margin-left: 5px;
}

</style>